﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrintRemoteObject
    {
        public static PrintRemote[] GetPrintRemotes()
        {
            return Query(null);
        }

        public static PrintRemote GetPrintRemote(int id)
        {
            PrintRemote printRemote = new PrintRemote();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintRemoteDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintRemoteDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printRemotes = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printRemote.Id                = Convert.ToInt32(printRemotes.Rows[0][PrintRemoteDesc.Id]);
            printRemote.FactoryId         = Convert.ToInt32(printRemotes.Rows[0][PrintRemoteDesc.FactoryId]);
            printRemote.PrinterQueueId    = Convert.ToInt32(printRemotes.Rows[0][PrintRemoteDesc.PrinterQueueId]);

            return printRemote;
        }

        public static PrintRemote[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrintRemoteDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                PrintRemote[] printRemotes = new PrintRemote[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrintRemote printRemote = new PrintRemote();

                    printRemote.Id                = Convert.ToInt32(table.Rows[i][PrintRemoteDesc.Id]);
                    printRemote.FactoryId         = Convert.ToInt32(table.Rows[i][PrintRemoteDesc.FactoryId]);
                    printRemote.PrinterQueueId    = Convert.ToInt32(table.Rows[i][PrintRemoteDesc.PrinterQueueId]);


                    printRemotes.SetValue(printRemote, i);
                }

                return printRemotes;
            }
            return null;
        }

        public static Int32 Add(PrintRemote printRemote)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrintRemoteDesc.FactoryId,              printRemote.FactoryId);
            builder.InsertAdd(PrintRemoteDesc.PrinterQueueId,         printRemote.PrinterQueueId);
            builder.QueryTableName = PrintRemoteDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintRemoteDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintRemoteDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrintRemote printRemote)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrintRemoteDesc.FactoryId,              printRemote.FactoryId);
            builder.UpdateAdd(PrintRemoteDesc.PrinterQueueId,         printRemote.PrinterQueueId);
            builder.ConditionAdd(PrintRemoteDesc.Id, ConditionExpress.EQUALS, printRemote.Id);
            builder.QueryTableName = PrintRemoteDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
